package com.sr.controller;

import com.sr.constant.Constant;
import com.sr.enums.ResponseCode;
import com.sr.ibase.IBaseService;
import com.sr.pojo.WMemberLevel;
import com.sr.service.IMemberLevelService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.MemberLevelReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiaochi
 * @date 2022/4/27 18:39
 * @desc MemberLevelController
 */
@Api(tags = "后台接口 -> 会员等级")
@RestController
@RequestMapping(Constant.Api.BACK_PREFIX +"/memberLevel")
@RequiredArgsConstructor(onConstructor = @__)
public class MemberLevelController extends BaseController{

    private final IMemberLevelService memberLevelService;

    /**
     * 查询所有会员等级
     * @return
     */
    @ApiOperation(value = "查询所有会员等级",notes = "查询所有会员等级")
    @GetMapping("/list/all")
    public R<List<WMemberLevel>> list(){
        IBaseService.Criteria<WMemberLevel> criteria = memberLevelService.createCriteria(WMemberLevel.class);
        criteria.andEqualTo(WMemberLevel::getIsDel,1);
        return R.ok(memberLevelService.findByWhere(criteria));
    }

    /**
     * 条件查询会员等级
     * @return
     */
    @ApiOperation(value = "分页查询会员等级",notes = "分页查询会员等级")
    @PostMapping("/list/page")
    public R<PageVo<WMemberLevel>> pageList(@RequestBody MemberLevelReqVo reqVo){
        IBaseService.Criteria<WMemberLevel> criteria = memberLevelService.createCriteria(WMemberLevel.class);
        criteria.andEqualTo(WMemberLevel::getName,!StringUtils.isEmpty(reqVo.getName()),reqVo.getName());
        return R.ok(memberLevelService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    /**
     * 新增会员等级
     * @return
     */
    @ApiOperation(value = "新增会员等级",notes = "新增会员等级")
    @PostMapping("/create")
    public R<String> create(@RequestBody @Validated(MemberLevelReqVo.Create.class) MemberLevelReqVo reqVo){
        if (memberLevelService.hasExistsByName(reqVo)){
            return R.error(ResponseCode.NAME_EXISTS_ERROR.getMessage());
        }
        return this.response(memberLevelService.insert(reqVo.buildEntity()));
    }

    /**
     * 更新会员等级
     * @return
     */
    @ApiOperation(value = "更新会员等级",notes = "更新会员等级")
    @PostMapping("/update")
    public R<String> update(@RequestBody @Validated(MemberLevelReqVo.Update.class) MemberLevelReqVo reqVo){
        if (memberLevelService.hasExistsByName(reqVo)){
            return R.error(ResponseCode.NAME_EXISTS_ERROR.getMessage());
        }
        return this.response(memberLevelService.updateById(reqVo.buildEntity()));
    }

    /**
     * 删除会员等级
     * @return
     */
    @ApiOperation(value = "删除会员等级",notes = "删除会员等级")
    @GetMapping("/delete/{id}")
    public R<String> delete(@PathVariable String id){
        return this.response(memberLevelService.updateById(WMemberLevel.builder().id(id).isDel(0).build()));
    }
}